package javagprssrv.Listener;

import javagprssrv.JGS_Param;
import javagprssrv.Log;
import java.net.ServerSocket;

public class TrdControlListener extends java.lang.Thread
{
    private int SocketIndex;
    private java.lang.String SocketName;
    private java.lang.String SocketProto;
    
    public TrdControlListener(final java.lang.String _Protocal, final int _SocketIndex, final java.lang.String _SocketName) {
        super("TrdControlListener");
        this.SocketIndex = -1;
        this.SocketName = "";
        this.SocketProto = "";
        this.SocketProto = _Protocal;
        this.SocketIndex = _SocketIndex;
        this.SocketName = _SocketName;
    }
    
    @java.lang.Override
    public void run() {
        try {
            if (this.SocketProto.equals("TCP")) {
                final java.net.ServerSocket server = new java.net.ServerSocket(this.SocketIndex);
                boolean isBounded;
                if (server.isBound()) {
                    isBounded = true;
                    new javagprssrv.Log().ShowLog(this.SocketName + "(" + this.SocketProto + ":" + java.lang.Integer.valueOf(this.SocketIndex).toString() + ") 打开成功。您可以使用 telnet <HostAdress> " + java.lang.Integer.valueOf(this.SocketIndex).toString() + "进行通讯。", true, true);
                }
                else {
                    isBounded = false;
                    java.lang.String strERR = "";
                    final java.lang.String strLine = java.lang.System.getProperty("line.separator");
                    strERR = this.SocketName + "(" + this.SocketProto + ":" + java.lang.Integer.valueOf(this.SocketIndex).toString() + ") 打开失败！" + strLine;
                    strERR = strERR + "                    (建议)请使用相关Dos命令,查看" + java.lang.Integer.valueOf(this.SocketIndex).toString() + "端口是否已经被使用。";
                    javagprssrv.JGS_Param.myLog.ShowLog(strERR, true, true);
                }
                while (isBounded) {
                    final javagprssrv.Listener.TrdControlTranser_TCP TCT_T = new javagprssrv.Listener.TrdControlTranser_TCP(server.accept());
                    TCT_T.start();
                }
            }
            if (this.SocketProto.equals("UDP")) {
                java.lang.String strERR2 = "";
                final java.lang.String strLine2 = java.lang.System.getProperty("line.separator");
                strERR2 = "系统暂不支持UDP协议的控制端口开放，" + strLine2;
                strERR2 = strERR2 + "                    故UDP:" + java.lang.Integer.valueOf(this.SocketIndex).toString() + "端口无法被打开。";
                javagprssrv.JGS_Param.myLog.ShowLog(strERR2, true, true);
            }
        }
        catch (java.lang.Exception e) {
            java.lang.String strERR = "";
            final java.lang.String strLine = java.lang.System.getProperty("line.separator");
            strERR = this.SocketName + "(" + this.SocketProto + ":" + java.lang.Integer.valueOf(this.SocketIndex).toString() + ") 打开失败！" + strLine;
            strERR = strERR + "                    (建议)请使用“netstat -an”命令,查看" + java.lang.Integer.valueOf(this.SocketIndex).toString() + "端口是否已经被使用。";
            javagprssrv.JGS_Param.myLog.ShowLog(strERR, true, true);
        }
    }
}
